package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.myapplication.util.saveSoul;


public class LoginPage extends AppCompatActivity {
    private EditText username;
    private EditText userpassword;
    private Button loginbtn;
    private TextView loginNow;
    private String usr;
    private String psw;
    private CheckBox rembpwd;
    private SharedPreferences.Editor editor;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginpage_activity);
        sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
        editor = sharedPreferences.edit();
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        username = (EditText) findViewById(R.id.user_et);
        userpassword = (EditText) findViewById(R.id.password_et);
        loginbtn = (Button) findViewById(R.id.login_btn);
        loginNow = (TextView) findViewById(R.id.login_now);
        rembpwd = findViewById(R.id.remb_pwd);
    }

    private void initData() {
        usr = username.getText().toString()+"";
        psw = userpassword.getText().toString()+"";
        int s = sharedPreferences.getInt("s",0);

        if(s == 1){
            String usr = sharedPreferences.getString("usr","");
            String psw = sharedPreferences.getString("psw","");
            username.setText(usr+"");
            userpassword.setText(psw+"");
        }



//        String username = saveSoul.loginSelect(username,"username");
//        String password = saveSoul.loginSelect(password,"password");
//        Toast.makeText(this, "账号："+username+"密码："+password, Toast.LENGTH_SHORT).show();
    }

    private void initEvent() {
        loginbtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                initData();
                if (usr == "" || psw == "") {
                    Toast.makeText(LoginPage.this, "账号密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    saveSoul db = new saveSoul(LoginPage.this);
                    Boolean result = db.loginSelect(usr, psw);
                    if (result) {
                        int s = sharedPreferences.getInt("s",0);
                        if(s == 1) {
                            editor.putString("usr", usr);
                            editor.putString("psw", psw);
                            editor.commit();
                        }
                        Toast.makeText(LoginPage.this, "登录成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(LoginPage.this, MainActivity.class);
                        intent.putExtra("usr",usr);
                        startActivity(intent);
                    } else {
                        Toast.makeText(LoginPage.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });


        loginNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent =new Intent(LoginPage.this,DB1.class);
                startActivity(intent);
            }
        });
      rembpwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              if (isChecked){
                  editor.putInt("s",1);
                  editor.commit();
              }else {
                  editor.putInt("s",0);
                  editor.commit();
              }
          }
      });

    }
}